﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;
using System.Data;

public partial class PopUp_CreateWorkItem : SemiCASEBasePage
{
    private string ProjectID
    {
        get
        {
            return Request.QueryString["ProjectID"] != null ? Request.QueryString["ProjectID"] : String.Empty;
        }
    }

    private string UseCaseID
    {
        get
        {
            return Request.QueryString["UseCaseID"] != null ? Request.QueryString["UseCaseID"] : String.Empty;
        }
    }

    private string WorkItemID
    {
        get
        {
            return Request.QueryString["WorkItemID"] != null ? Request.QueryString["WorkItemID"] : String.Empty;
        }
    }

    private string ScreenMode
    {
        get
        {
            return Request.QueryString["ScreenMode"] != null ? Request.QueryString["ScreenMode"] : String.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadPercentage();
            LoadExtentions();
            SetScreenMode();
        }
    }

    private void LoadPercentage()
    {
        DrpDownListPercentage.Items.Clear();

        for (int i = 1; i < 101; i++)
        {
            DrpDownListPercentage.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }

    protected void LinkButtonNavigateWorkItems_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/PopUp/WorkItemList.aspx" + "?UseCaseID=" + this.UseCaseID + "&ProjectID=" + this.ProjectID);
    }

    private void SetScreenMode()
    {
        if (this.ScreenMode == "Create")
        {
            ThBaslik.InnerText = "CREATE WORK ITEM";
        }
        else if (this.ScreenMode == "Update")
        {
            ThBaslik.InnerText = "UPDATE WORK ITEM";
            LoadWorkItemInfo();
        }
    }

    private void LoadExtentions()
    {
        string ucName = String.Empty;
        DrpDownListExt.Items.Clear();

        if (Session["UseCaseList"] != null)
        {
            DataTable dtUC = (DataTable)Session["UseCaseList"];
            foreach (DataRow row in dtUC.Rows)
            {
                if (row["UseCaseID"].ToString() == this.UseCaseID)
                {
                    ucName = row["Name"].ToString();
                    DrpDownListExt.Items.Add(new ListItem(row["Name"].ToString(), "UC"));
                    DrpDownListExt.Items.Add(new ListItem(row["Name"].ToString() + " - Mss", "MSS"));
                    break;
                }
            }
        }

        DataTable dt = new ExtentionBS().LoadExtentionsFromDB(new Guid(this.ProjectID), new Guid(this.UseCaseID));

        foreach (DataRow row in dt.Rows)
        {
            DrpDownListExt.Items.Add(new ListItem(ucName + " - " + row["MssSiraNo"].ToString() + "." + row["OrderSign"] + " " + row["Name"].ToString(), row["ExtentionID"].ToString()));
        }

        DrpDownListExt.Items.Insert(0, new ListItem("Select Relation", "0"));
    }

    private void LoadWorkItemInfo()
    {
        DataTable dt = new WorkItemBS().GetWorkItemInfo(new Guid(this.WorkItemID));

        if (dt.Rows.Count == 0)
        {
            MessageBox("There is no defined work item info", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return;
        }

        DataRow row = dt.Rows[0];

        GenelTextBoxName.Text = (row["Name"] != DBNull.Value) ? row["Name"].ToString() : String.Empty;
        GenelTextBoxPredecessor.Text = (row["Predecessor"] != DBNull.Value) ? row["Predecessor"].ToString() : String.Empty;
        GenelTextBoxResources.Text = (row["Resources"] != DBNull.Value) ? row["Resources"].ToString() : String.Empty;
        TarihStartDate.ucoText = (row["StartDate"] != DBNull.Value) ? row["StartDate"].ToString() : String.Empty;
        TarihEndDate.ucoText = (row["EndDate"] != DBNull.Value) ? row["EndDate"].ToString() : String.Empty;
        if (row["Percentage"] != DBNull.Value)
        {
            DrpDownListPercentage.SelectedIndex = -1;
            (DrpDownListPercentage.Items.FindByValue(row["Percentage"].ToString())).Selected = true;
        }

        if (row["ExtentionID"] != DBNull.Value)
        {
            DrpDownListExt.SelectedIndex = -1;
            (DrpDownListExt.Items.FindByValue(row["ExtentionID"].ToString())).Selected = true;
        }
        else if (row["MssID"] != DBNull.Value)
        {
            DrpDownListExt.SelectedIndex = -1;
            (DrpDownListExt.Items.FindByValue("MSS")).Selected = true;
        }
        else
        {
            DrpDownListExt.SelectedIndex = -1;
            (DrpDownListExt.Items.FindByValue("UC")).Selected = true;
        }
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (this.ScreenMode == "Create")
            CreateWorkItem();
        else if (this.ScreenMode == "Update")
        {
            UpdateWorkItem();
        }
    }

    private void UpdateWorkItem()
    {
        Guid? pMssID = null;
        Guid? pExtentionID = null;
        Guid? pExtentionStepID = null;

        if (DrpDownListExt.SelectedItem != null && DrpDownListExt.SelectedItem.Value != "0")
        {
            if (DrpDownListExt.SelectedIndex == 1)
            {
                pMssID = null;
            }
            else if (DrpDownListExt.SelectedIndex == 2)
            {
                DataTable dt = new MainSuccessBS().LoadMSSFromDB(new Guid(this.ProjectID), new Guid(this.UseCaseID));
                pMssID = new Guid(dt.Rows[0]["MssID"].ToString());
            }
            else
                pExtentionID = new Guid(DrpDownListExt.SelectedItem.Value);
        }

        new WorkItemBS().UpdateWorkItem(new Guid(this.ProjectID),
                                        new Guid(this.UseCaseID),
                                         pMssID,
                                        pExtentionID,
                                        pExtentionStepID,
                                        new Guid(this.WorkItemID),
                                        GenelTextBoxName.Text,
                                        TarihStartDate.ucoTarih,
                                        TarihEndDate.ucoTarih,
                                        GenelTextBoxResources.Text,
                                        GenelTextBoxPredecessor.Text,
                                        Convert.ToInt32(DrpDownListPercentage.SelectedItem.Value));

        Response.Redirect("~/PopUp/WorkItemList.aspx" + "?UseCaseID=" + this.UseCaseID + "&ProjectID=" + this.ProjectID);
    }

    private void CreateWorkItem()
    {
        int? WorkItemListCount = (int?)Session["WorkItemListCount"];
        if (WorkItemListCount == null)
            WorkItemListCount = 0;

        Guid? pMssID = null;
        Guid? pExtentionID = null;
        Guid? pExtentionStepID = null;


        if (DrpDownListExt.SelectedItem != null && DrpDownListExt.SelectedItem.Value != "0")
        {
            if (DrpDownListExt.SelectedIndex == 1)
            {
                pMssID = null;
            }
            else if (DrpDownListExt.SelectedIndex == 2)
            {
                DataTable dt = new MainSuccessBS().LoadMSSFromDB(new Guid(this.ProjectID), new Guid(this.UseCaseID));
                pMssID = new Guid(dt.Rows[0]["MssID"].ToString());
            }
            else
                pExtentionID = new Guid(DrpDownListExt.SelectedItem.Value);
        }

        new WorkItemBS().CreateWorkItem(new Guid(this.ProjectID),
                                        new Guid(this.UseCaseID),
                                         pMssID,
                                        pExtentionID,
                                        pExtentionStepID,
                                        (WorkItemListCount + 1).ToString(),
                                        GenelTextBoxName.Text,
                                        TarihStartDate.ucoTarih,
                                        TarihEndDate.ucoTarih,
                                        GenelTextBoxResources.Text,
                                        GenelTextBoxPredecessor.Text,
                                        Convert.ToInt32(DrpDownListPercentage.SelectedItem.Value));

        Response.Redirect("~/PopUp/WorkItemList.aspx" + "?UseCaseID=" + this.UseCaseID + "&ProjectID=" + this.ProjectID);
    }

}